var bookIsbn = $.cookie("bookIsbn");
var book = getBookByIsbn(bookIsbn);
var reg = new RegExp("\"", "g");
let storeBook = null;

function getBookByIsbn(Isbn) {
    var result;
    var formData = new FormData();
    formData.append("Isbn", Isbn);
    $.ajax({
        type: "post",
        url: "/api/book/v/getBookByIsbn",
        async: false,
        dataType: "text",
        data: formData,
        processData: false,
        contentType: false,
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                result = JSON.parse(data.data);
            }else {
                result = [];
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    });
    return result;
}

function getBookByCategory(categoryName) {
    var result;
    var formData = new FormData();
    formData.append("categoryName", categoryName);
    formData.append("pageNum", 1);
    formData.append("pageSize", 5);
    $.ajax({
        type: "post",
        url: "/api/book/v/page/getBookByCategory",
        async: false,
        dataType: "text",
        data: formData,
        processData: false,
        contentType: false,
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                result = JSON.parse(data.data);
            }else {
                result = [];
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    });
    return result;
}

function getStoreBookByIsbn(Isbn) {
    var result = [];
    var formData = new FormData();
    formData.append("Isbn", Isbn);
    $.ajax({
        type: "post",
        url: "/api/storeBook/v/getStoreBookByIsbn",
        async: false,
        dataType: "text",
        data: formData,
        processData: false,
        contentType: false,
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                result = JSON.parse(data.data);
            }else {
                result = [];
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    });
    return result;
}

function getOtherBookTemplate(book) {
    return "<div class='recent-post' style='margin-bottom: 10px;'>" +
        "<div class='recent-post'>" +
        "<a href='bookDetails?bookIsbn=" + book.bookIsbn + "'>" +
        "<img class='img-full' src='..//..//images//" + book.bookImg.replace(reg, "") + "' alt='Umino's Blog Image'>" +
        "</a>" +
        "</div>" +
        "<div class='recent-post_desc'>" +
        "<span><a href='bookDetails?bookIsbn=" + book.bookIsbn + "'>" + book.bookName + "</a></span>" +
        "<span class='post-date'>" + book.bookTime + "</span>" +
        "</div>" +
        "</div>";
}

function getStoreBookTemplate(book) {
    return "<div class='col-lg-4' ind='"+ book.storeId +"'>" +
        "<div class='related-post_info'>" +
        "<div class='related-post_img'>" +
        "<a href='purchase?storeId="+ book.storeId +"&Isbn=" + book.bookIsbn + "' isbn='" + book.bookIsbn + "'>" +
        "<img style='height: 270px; width: auto' src='..//..//images//" + book.img.replace(reg, "") + "' alt='Umino's Blog Image'>" +
        "</a>" +
        "</div>" +
        "<div class='related-post_content'>" +
        "<h3 class='title'>" + book.storeName + "</h3>" +
        "<span>价格 ￥<strong>" + book.discountPrice + "</strong></span>" +
        "</div>" +
        "</div>" +
        "</div>";
}

$(document).ready(function () {
    $("div.blog-img>img").attr("src", "..//..//images//" + book.bookImg.replace(reg, ""));
    $("li[name='category']>a").text(book.categoryName);
    $("li[name='author']>a").text(book.bookAuthor);
    $("li[name='price']>a").text(book.bookPrice);
    $("li[name='publisher']>a").text(book.bookPublishers);
    $("li[name='time']>a").text(book.bookTime);
    $("li[name='Isbn']>a").text(book.bookIsbn);
    $("div[name='bookName']>h3").text(book.bookName);
    $("div.umino-blog-blockquote>blockquote>p").text(book.bookIntroduction);

    var otherBook = getBookByCategory(book.categoryName);
    otherBook.forEach(book => {
        $("div[name='bookByCategory']").append(getOtherBookTemplate(book));
    });

    storeBook = getStoreBookByIsbn(book.bookIsbn);
    storeBook.forEach(book => {
        $("div[name='storeBook'] div.row").append(getStoreBookTemplate(book));
    })
   $("div.related-post_img>a").on("click", function () {
       let isbn = $(this).attr("isbn");
       storeBook.forEach(book => {
           if (book.bookIsbn == isbn) {
               $.cookie("storeBook", book);
               location.href="wish"
           }
       })
   })
})